﻿using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace classTest
{

    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class OptionsWindows : Window
    {
        private SimpleMarkerSymbol pointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Circle, System.Drawing.Color.Purple, 3);
        private System.Drawing.Color pointColor;
        private SimpleMarkerSymbolStyle pointStyle;

        private double pointSize;
        private SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, System.Drawing.Color.Green, 3);
        private System.Drawing.Color lineColor;
        private SimpleLineSymbolStyle lineStyle;

        private double lineSize;
        private SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, System.Drawing.Color.Cyan, new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, System.Drawing.Color.DarkBlue, 2));
        private SimpleLineSymbol outlineSymbol;
        private System.Drawing.Color fillColor;
        private SimpleFillSymbolStyle fillStyle;
        private System.Drawing.Color outlineColor;
        private SimpleLineSymbolStyle outlineStyle;
        private double outlineSize;
        //bool beoutlineCheckbox;

        private Renderer pSymbol;


        public FeatureLayer pLayer { get; set; }

        //初始化
        public OptionsWindows(FeatureLayer pLayer1)
        {
            InitializeComponent();
            pLayer = pLayer1;
            pSymbol = pLayer.Renderer;
        }


        /*窗体设置*/
        //为窗体的 Loaded 事件添加代码，主要是获取另一个窗口里的控件属性设置
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //获取图层的原始符号样式，并传递给窗口的相关控件给予显示
            SimpleRenderer ssRenderer = pLayer.Renderer as SimpleRenderer;
            //if判断需要符号化的图层是什么类型，窗口就只显示什么类型的符号设计窗口，用户自定义创建符号，并赋值给图层
            if (pLayer.FeatureTable.GeometryType == GeometryType.Point || pLayer.FeatureTable.GeometryType == GeometryType.Multipoint)
            {
                if (ssRenderer != null)
                {
                    if (ssRenderer.Symbol as SimpleMarkerSymbol != null)
                    {
                        pointSymbol = ssRenderer.Symbol.Clone() as SimpleMarkerSymbol;
                    }

                }
                fillPropertyTab.Visibility = Visibility.Collapsed;
                linePropertyTab.Visibility = Visibility.Collapsed;
                tabControl.SelectedIndex = 0;
            }
            else if (pLayer.FeatureTable.GeometryType == GeometryType.Polyline)
            {
                if (ssRenderer != null)
                {
                    //Esri.ArcGISRuntime.Symbology.MultilayerPolylineSymbol 什么玩意？？？
                    if (ssRenderer.Symbol as SimpleLineSymbol != null)
                    {
                        lineSymbol = (SimpleLineSymbol)ssRenderer.Symbol.Clone();
                    }
                }
                pointPropertyTab.Visibility = Visibility.Collapsed;
                fillPropertyTab.Visibility = Visibility.Collapsed;
                tabControl.SelectedIndex = 1;
            }
            else if (pLayer.FeatureTable.GeometryType == GeometryType.Polygon)
            {
                if (ssRenderer != null)
                {
                    if (ssRenderer.Symbol as SimpleFillSymbol != null)
                    {
                        fillSymbol = ssRenderer.Symbol.Clone() as SimpleFillSymbol;
                    }
                }
                pointPropertyTab.Visibility = Visibility.Collapsed;
                linePropertyTab.Visibility = Visibility.Collapsed;
                tabControl.SelectedIndex = 2;
            }

            if (pointSymbol != null)
            {
                pointColor = pointSymbol.Color;
                pointStyle = pointSymbol.Style;
                pointSize = Math.Round(pointSymbol.Size,2);
            }
            if (lineSymbol != null)
            {
                lineColor = lineSymbol.Color;
                lineStyle = lineSymbol.Style;
                lineSize = Math.Round(lineSymbol.Width, 2);

            }
            if (fillSymbol != null)
            {
                fillColor = fillSymbol.Color;
                fillStyle = fillSymbol.Style;
                
                if (fillSymbol.Outline != null)
                {
                    outlineSymbol = (SimpleLineSymbol)fillSymbol.Outline;
                    outlineCheckBox.IsChecked = true;
                    outlineStyle = outlineSymbol.Style;
                    outlineSize = Math.Round(outlineSymbol.Width,2);
                    outlineColor = outlineSymbol.Color;
                    outlineCheckBox.IsChecked = true;
                }
                else
                {
                    outlineSymbol = null;
                    outlineCheckBox.IsChecked = false;
                }
                //beoutlineCheckbox = (bool)outlineCheckBox.IsChecked;
            }
        }

        /*点符号设置*/

        //为“点符号”tab 页的 Loaded 事件添加代码
        private void PointPropertyTab_Loaded(object sender, RoutedEventArgs e)
        {
            switch (pointStyle)
            {
                case SimpleMarkerSymbolStyle.Circle:
                    radioButton1.IsChecked = true;
                    break;
                case SimpleMarkerSymbolStyle.Cross:
                    radioButton2.IsChecked = true;
                    break;
                case SimpleMarkerSymbolStyle.Diamond:
                    radioButton3.IsChecked = true;
                    break;
                case SimpleMarkerSymbolStyle.Square:
                    radioButton4.IsChecked = true;
                    break;
                case SimpleMarkerSymbolStyle.X:
                    radioButton5.IsChecked = true;
                    break;
                case SimpleMarkerSymbolStyle.Triangle:
                    radioButton6.IsChecked = true;
                    break;
            }
            sizeSlider.Value = pointSize;
            Color c = Color.FromArgb(pointColor.A, pointColor.R, pointColor.G, pointColor.B);
            colorLabel.Background = new SolidColorBrush(c);
        }

        //为“点符号”tab 页中的 radiobutton 的 checked 事件添加代码
        private void radioButton1_Checked(object sender, RoutedEventArgs e)
        {
            pointSymbol.Style = SimpleMarkerSymbolStyle.Circle;
        }
        private void radioButton2_Checked(object sender, RoutedEventArgs e)
        {
            pointSymbol.Style = SimpleMarkerSymbolStyle.Cross;
        }
        private void radioButton3_Checked(object sender, RoutedEventArgs e)
        {
            pointSymbol.Style = SimpleMarkerSymbolStyle.Diamond;
        }
        private void radioButton4_Checked(object sender, RoutedEventArgs e)
        {
            pointSymbol.Style = SimpleMarkerSymbolStyle.Square;
        }
        private void radioButton5_Checked(object sender, RoutedEventArgs e)
        {
            pointSymbol.Style = SimpleMarkerSymbolStyle.X;
        }
        private void radioButton6_Checked(object sender, RoutedEventArgs e)
        {
            pointSymbol.Style = SimpleMarkerSymbolStyle.Triangle;
        }

        //为“点符号”tab 页的 silder 控件的 ValueChanged 事件添加代码
        private void sizeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            double v = e.NewValue;
            sizeLabel.Content = "大小：" + v.ToString();
            pointSymbol.Size = v;
        }

        //为“点符号”tab 页的颜色 label 控件的 MouseDoubleClick 事件添加代码，设置颜色
        private void colorLabel_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Forms.ColorDialog clrDlg = new System.Windows.Forms.ColorDialog();
            SolidColorBrush s = (SolidColorBrush)colorLabel.Background;
            clrDlg.Color = System.Drawing.Color.FromArgb(s.Color.A, s.Color.R, s.Color.G, s.Color.B);
            if (clrDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                pointSymbol.Color = clrDlg.Color;
                Color c = Color.FromArgb(clrDlg.Color.A, clrDlg.Color.R, clrDlg.Color.G, clrDlg.Color.B);
                colorLabel.Background = new SolidColorBrush(c);
            }
        }

        //为“点符号”tab 页中的三个 button 的点击事件添加代码
        private void OKBtn_Click(object sender, RoutedEventArgs e)
        {
            pLayer.Renderer = new SimpleRenderer(pointSymbol);
            this.Close();
            //this.DialogResult = true;

        }
        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            pLayer.Renderer = pSymbol;
            this.Close();
        }
        private void ApplyBtn_Click(object sender, RoutedEventArgs e)
        {
            pLayer.Renderer = new SimpleRenderer(pointSymbol);
        }


        /*线符号设置*/

        //为“线符号”tab 页的 Loaded 事件添加代码
        private void linePropertyTab_Loaded(object sender, RoutedEventArgs e)
        {
            switch (lineStyle)
            {
                case SimpleLineSymbolStyle.Solid:
                    lineStyleCombobox.SelectedIndex = 0;
                    break;
                case SimpleLineSymbolStyle.Dash:
                    lineStyleCombobox.SelectedIndex = 1;
                    break;
                case SimpleLineSymbolStyle.Dot:
                    lineStyleCombobox.SelectedIndex = 2;
                    break;
                case SimpleLineSymbolStyle.DashDot:
                    lineStyleCombobox.SelectedIndex = 3;
                    break;
                case SimpleLineSymbolStyle.DashDotDot:
                    lineStyleCombobox.SelectedIndex = 4;
                    break;
                case SimpleLineSymbolStyle.Null:
                    lineStyleCombobox.SelectedIndex = -1;
                    break;
            }
            lineWidthSlider.Value = lineSize;
            Color curColor = Color.FromArgb(lineColor.A, lineColor.R, lineColor.G, lineColor.B);
            lineColorRectangle.Fill = new SolidColorBrush(curColor);
        }

        //为“线符号”tab 页中的 combobox 控件添加 SelectionChanged 事件添加代码
        private void lineStyleCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = lineStyleCombobox.SelectedIndex;
            switch (index)
            {
                case 0:
                    lineSymbol.Style = SimpleLineSymbolStyle.Solid;
                    break;
                case 1:
                    lineSymbol.Style = SimpleLineSymbolStyle.Dash;
                    break;
                case 2:
                    lineSymbol.Style = SimpleLineSymbolStyle.Dot;
                    break;
                case 3:
                    lineSymbol.Style = SimpleLineSymbolStyle.DashDot;
                    break;
                case 4:
                    lineSymbol.Style = SimpleLineSymbolStyle.DashDotDot;
                    break;
                default:
                    lineSymbol.Style = SimpleLineSymbolStyle.Null;
                    break;
            }

        }

        //为“线符号”tab 页中的 slider 控件的 ValueChanged 事件添加代码
        private void lineWidthSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            double v = e.NewValue;
            lineSizeLabel.Content = "宽度：" + v.ToString();
            lineSymbol.Width = v;
        }

        //为“线符号”tab 页中的 Rectangle 控件的 MouseDown 事件添加代码，设置填充颜色   
        private void LineColorRectangle_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                System.Windows.Forms.ColorDialog clrDlg = new System.Windows.Forms.ColorDialog();
                SolidColorBrush br = (SolidColorBrush)lineColorRectangle.Fill;
                clrDlg.Color = System.Drawing.Color.FromArgb(br.Color.A, br.Color.R, br.Color.G, br.Color.B);
                if (clrDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Color selColor = Color.FromArgb(clrDlg.Color.A, clrDlg.Color.R, clrDlg.Color.G, clrDlg.Color.B);
                    lineColorRectangle.Fill = new SolidColorBrush(selColor);
                    lineSymbol.Color = clrDlg.Color;
                }
            }
        }

        //为“线符号”tab 页中的三个按钮添加点击事件处理代码
        private void lineOKBtn_Click(object sender, RoutedEventArgs e)
        {
            pLayer.Renderer = new SimpleRenderer(lineSymbol);
            this.Close();
        }
        private void lineCancelBtn_Click(object sender, RoutedEventArgs e)
        {
            pLayer.Renderer = pSymbol;
            this.Close();
        }
        private void lineApplyBtn_Click(object sender, RoutedEventArgs e)
        {
            pLayer.Renderer = new SimpleRenderer(lineSymbol);
        }


        /*面符号设置*/

        //为“面符号”tab 页的 Loaded 事件添加代码
        private void fillPropertyTab_Loaded(object sender, RoutedEventArgs e)
        {
            switch (fillStyle)
            {
                case SimpleFillSymbolStyle.DiagonalCross:
                    fillStyleListBox.SelectedIndex = 0;
                    break;
                case SimpleFillSymbolStyle.Horizontal:
                    fillStyleListBox.SelectedIndex = 1;
                    break;
                case SimpleFillSymbolStyle.Null:
                    fillStyleListBox.SelectedIndex = 2;
                    break;
                case SimpleFillSymbolStyle.Vertical:
                    fillStyleListBox.SelectedIndex = 3;
                    break;
                case SimpleFillSymbolStyle.Solid:
                    fillStyleListBox.SelectedIndex = 4;
                    break;
                case SimpleFillSymbolStyle.Cross:
                    fillStyleListBox.SelectedIndex = 5;
                    break;
                case SimpleFillSymbolStyle.ForwardDiagonal:
                    fillStyleListBox.SelectedIndex = 6;
                    break;
                case SimpleFillSymbolStyle.BackwardDiagonal:
                    fillStyleListBox.SelectedIndex = 7;
                    break;
            }
            if (outlineSymbol != null)
            {
                switch (outlineStyle)
                {
                    case SimpleLineSymbolStyle.Solid:
                        outlineStyleComboBox.SelectedIndex = 0;
                        break;
                    case SimpleLineSymbolStyle.Dash:
                        outlineStyleComboBox.SelectedIndex = 1;
                        break;
                    case SimpleLineSymbolStyle.Dot:
                        outlineStyleComboBox.SelectedIndex = 2;
                        break;
                    case SimpleLineSymbolStyle.DashDot:
                        outlineStyleComboBox.SelectedIndex = 3;
                        break;
                    case SimpleLineSymbolStyle.DashDotDot:
                        outlineStyleComboBox.SelectedIndex = 4;
                        break;
                    case SimpleLineSymbolStyle.Null:
                        outlineStyleComboBox.SelectedIndex = -1;
                        break;
                }
                outlineTextBox.Text = outlineSize.ToString();
                Color vr = Color.FromArgb(outlineColor.A, outlineColor.R, outlineColor.G, outlineColor.B);
                outlineColorRectangle.Fill = new SolidColorBrush(vr);
            }
            else
            {
                outlineColorRectangle.Fill = new SolidColorBrush(Colors.Aqua);
                outlineStyleComboBox.SelectedIndex = 0;
                outlineTextBox.Text = "1";
                outlineSize = 1;
                outlineStyle = SimpleLineSymbolStyle.Solid;
                outlineColor = System.Drawing.Color.Aqua;
            }


            Color vc = Color.FromArgb(fillColor.A, fillColor.R, fillColor.G, fillColor.B);
            fillColorCanvas.Background = new SolidColorBrush(vc);
        }

        //为“面符号”tab 页中的 combobox 控件添加 SelectionChanged 事件添加代码，设置图形填充样式
        private void fillStyleListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = fillStyleListBox.SelectedIndex;
            switch (index)
            {
                case 0:
                    fillSymbol.Style = SimpleFillSymbolStyle.DiagonalCross;
                    break;
                case 1:
                    fillSymbol.Style = SimpleFillSymbolStyle.Horizontal;
                    break;
                case 2:
                    fillSymbol.Style = SimpleFillSymbolStyle.Null;
                    break;
                case 3:
                    fillSymbol.Style = SimpleFillSymbolStyle.Vertical;
                    break;
                case 4:
                    fillSymbol.Style = SimpleFillSymbolStyle.Solid;
                    break;
                case 5:
                    fillSymbol.Style = SimpleFillSymbolStyle.Cross;
                    break;
                case 6:
                    fillSymbol.Style = SimpleFillSymbolStyle.ForwardDiagonal;
                    break;
                case 7:
                    fillSymbol.Style = SimpleFillSymbolStyle.BackwardDiagonal;
                    break;
                default:
                    fillSymbol.Style = SimpleFillSymbolStyle.Null;
                    break;
            }
        }

        //为“面符号”tab 页中的 combobox 控件添加 SelectionChanged 事件添加代码，设置边框线样式
        private void outlineStyleComoBoxBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = outlineStyleComboBox.SelectedIndex;
            switch (index)
            {
                case 0:
                    outlineStyle = SimpleLineSymbolStyle.Solid;
                    break;
                case 1:
                    outlineStyle = SimpleLineSymbolStyle.Dash;
                    break;
                case 2:
                    outlineStyle = SimpleLineSymbolStyle.Dot;
                    break;
                case 3:
                    outlineStyle = SimpleLineSymbolStyle.DashDot;
                    break;
                case 4:
                    outlineStyle = SimpleLineSymbolStyle.DashDotDot;
                    break;
                default:
                    outlineStyle = SimpleLineSymbolStyle.Null;
                    break;
            }
            if (outlineCheckBox.IsChecked == true)
            {
                fillSymbol.Outline = new SimpleLineSymbol(outlineStyle, outlineColor, outlineSize);
            }
            else
            {
                fillSymbol.Outline = null;
            }
        }

        //为“面符号”tab 页中的 CheckBox 控件添加 Checked 和 Unchecked 事件添加代码，监视边界线的方框是否打钩
        private void OutlineCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            fillSymbol.Outline = new SimpleLineSymbol(outlineStyle, outlineColor, outlineSize);
        }

        private void OutlineCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            fillSymbol.Outline = null;
        }

        //为“面符号”tab 页中的 TextBox 控件的 TextChanged 事件添加代码，设置边框线大小  
        private void OutlineTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                outlineSize = Convert.ToInt32(outlineTextBox.Text);
            }
            catch
            {
                outlineSize = 1;
            }
            if (outlineCheckBox.IsChecked == true)
            {
                fillSymbol.Outline = new SimpleLineSymbol(outlineStyle, outlineColor, outlineSize);
            }
            else
            {
                fillSymbol.Outline = null;
            }
        }

        //为“面符号”tab 页中的 Rectangle 控件的 MouseDown 事件添加代码，设置边框线颜色   
        private void OutlineColorRectangle_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                System.Windows.Forms.ColorDialog clrDlg = new System.Windows.Forms.ColorDialog();
                SolidColorBrush br = (SolidColorBrush)outlineColorRectangle.Fill;
                clrDlg.Color = System.Drawing.Color.FromArgb(br.Color.A, br.Color.R, br.Color.G, br.Color.B);
                if (clrDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Color selColor = Color.FromArgb(clrDlg.Color.A, clrDlg.Color.R, clrDlg.Color.G, clrDlg.Color.B);
                    outlineColorRectangle.Fill = new SolidColorBrush(selColor);
                    outlineColor = clrDlg.Color;
                    if (outlineCheckBox.IsChecked == true)
                    {
                        fillSymbol.Outline = new SimpleLineSymbol(outlineStyle, outlineColor, outlineSize);
                    }
                    else
                    {
                        fillSymbol.Outline = null;
                    }
                }
            }
        }

        //为“面符号”tab 页中的 Rectangle 控件的 MouseDown 事件添加代码，设置填充颜色   
        private void colorCanvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                System.Windows.Forms.ColorDialog clrDlg = new System.Windows.Forms.ColorDialog();
                SolidColorBrush br = (SolidColorBrush)fillColorCanvas.Background;
                clrDlg.Color = System.Drawing.Color.FromArgb(br.Color.A, br.Color.R, br.Color.G, br.Color.B);
                if (clrDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Color selColor = Color.FromArgb(clrDlg.Color.A, clrDlg.Color.R, clrDlg.Color.G, clrDlg.Color.B);
                    fillColorCanvas.Background = new SolidColorBrush(selColor);
                    fillSymbol.Color = clrDlg.Color;
                }
            }
        }

        //为“面符号”tab 页中的三个按钮添加点击事件处理代码
        private void fillOKBtn_Click(object sender, RoutedEventArgs e)
        {
            pLayer.Renderer = new SimpleRenderer(fillSymbol);
            this.Close();
        }
        private void fillCancelBtn_Click(object sender, RoutedEventArgs e)
        {
            pLayer.Renderer = pSymbol;
            this.Close();
        }
        private void fillApplyBtn_Click(object sender, RoutedEventArgs e)
        {
            pLayer.Renderer = new SimpleRenderer(fillSymbol);
        }
    }
}
